﻿define([
    'views/baseView',
    'models/crud/projectModel',
    'text!templates/crud/crudBase.html',
    'text!templates/crud/crudForm.html',
    'text!templates/crud/project/projectCreate.html',
], function (BaseView, ProjectModel, BaseTmp, FormTmp, UniqTmp) {
    var view = BaseView.extend({

        $form: null,

        render: function () {
            toggleLoading(true);
            $("div[data-rel='crud_project']").remove();

            var baseTemplate = Handlebars.compile(BaseTmp);
            //var formTemplate = Handlebars.compile(FormTmp);
            var uniqTemplate = Handlebars.compile(UniqTmp);

            var dataBaseOptions = {
                uuId: this._uId,
                dataRel: "crud_project", // cuidado, ref en js
                tmpClass: "project_new", // no esta en el html
                title: translate("_newProject")
            };

            var html = baseTemplate(dataBaseOptions);
            //var html2 = formTemplate();
            var html3 = uniqTemplate();

            this.$el.append(html);
            this.$el.find(".crud_content").append(html3);

            this.$form = this.$el.find("form");

            postRender(this.$el);

            this.addHandlers();
            toggleLoading(false);
        },
        save: function () {
            if (this.$form.parsley('validate')) {
                var that = this;
                var name = this.$form.find("input[name='name']").val();
                var description = this.$form.find("input[name='description']").val();
                //var active = this.$form.find("input[name='active']").bootstrapSwitch('state');

                var project = {
                    solutionId: that.options.solutionId,
                    name: name,
                    description: description//, deleted: active
                };
                var model = new ProjectModel();
                model.create(project, function (dataOk) {
                    //$("#sidebar").trigger("newSolution", ["solutionName", "solutionId", "solutionExtra"]);
                    that.cancel();
                }, function (dataError) { });
            }
        },
        // go back to solution manager
        cancel: function () {
            var that = this;
            require(['controllers/crud/projectMngr'], function (Controller) {
                (new Controller()).show(that.$el, that.options.solutionId);
            });
        },
        addHandlers: function () {
            this.$form.on("submit", $.proxy(function () {
                this.save();
                return false;
            }, this));
            //$("div[data-id='" + this._uId + "']").find("div.submit button.btn-cancel").on("click", $.proxy(function () {
            //    this.cancel();
            //    return false;
            //}, this));
            //$("div[data-id='" + this._uId + "']").find("div.abm-top-options a.btn-close").on("click", this.cancel);
            this.$form.find("button.btn-cancel").on("click", $.proxy(function () {
                this.cancel();
                return false;
            }, this));
        }
    });
    return view;
});